<?php
namespace app\index\logic;
class PeptideComponent{
	private $mMw;
	private $mAcidCount;
	private $mBaseCount;
	/**
	 * 纯度
	 */
	private $mPurity;
	/**
	 * 理论肽含量
	 */
	private $mY;
	/**
	 * 目标肽含量
	 */
	private $mLastPurity;
	
	private $mHasError=false;
	private $mMessage;
	
	public function calculate($saltId){
		$AminoSalt = model('AminoSalt');
		$amino_salt = $AminoSalt->where('id='.$saltId)->find();
		if(is_null($amino_salt)){
			$this->mHasError = true;
			$this->mMessage = '未能找到盐类型，无法计算肽含量';
			return;
		}
		
		$y = $this->getY($amino_salt, $this->mMw, $this->mAcidCount, $this->mBaseCount);
		$this->mLastPurity = $y * $this->mPurity;
		$this->mY = $y;
	}
	
	public function getResult(){
		$result = array(
		   'mw'=>$this->mMw,
		   'acidCount'=>$this->mAcidCount,
		   'baseCount'=>$this->mBaseCount,
		   'purity'=>$this->mPurity.'%',
		   'lastPurity'=>sprintf('%.0f', $this->mLastPurity).'%',
		   'has_error'=>$this->mHasError,
		   'message'=>$this->mMessage,
		   'y'=>sprintf('%.0f', ($this->mY)*100).'%'
		);
		return $result;
	}
	private function getY($aminoSalt, $mw, $acidCount, $baseCount){
		$tmp = $mw / ($mw + $aminoSalt['acid']*$acidCount + $aminoSalt['base']*$baseCount);
		$y = $tmp - 0.05;
		return $y;
	}

	public function __set($name, $value){
		$this->$name = $value;
	}

	public function __get($name){
		return $this->$name;
	}
}
